#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "cmsis_os2.h"
#include "ohos_init.h"

#include "iot_uart.h"
#include "NT1.h"
#include "utils.h"
#include "service.h"

#define NT1_RX_TASK_STACK_SIZE 1024 * 25
#define NT1_RX_TASK_PRIO 23
#define NT1_UART_BUFF_SIZE 16
#define NT1_UART_STR_SIZE 300
#define NT1_UART_Data_SIZE 300
#define NT1_FLAGS_MSK1 0x00000001U
#define NT1_DTU_UART 2

osThreadAttr_t NT1_attr;
osEventFlagsId_t NT1_evtID;

uint8_t NT1_uartBuff[NT1_UART_BUFF_SIZE];
uint8_t * NT1_uartBuffPtr = NT1_uartBuff;
uint8_t NT1_uartStr[NT1_UART_STR_SIZE];
uint8_t * NT1_uartStrPtr = NT1_uartStr;
uint8_t NT1_uartData[NT1_UART_Data_SIZE];
uint8_t * NT1_uartDataPtr = NT1_uartData;

// char NT1_hello[3] = {0xC3,0xC3,0xC3};
// char * NT1_helloPtr = NT1_hello;

// char * NT1_sockind = "admin,sockind,off\r\n";    // 关闭socket区分
// char * NT1_save = "admin,save\r\n";              // 保存配置
// char * NT1_sleep = "admin, powermode,0\r\n";     // 取消休眠模式

static int NT1_uartLength = 0;
static int NT1_uartEndDataLength = 0;

static void *NT1_Config_Task(const char *arg){
    (void)arg;

    NT1_uartLength = 0;

    while (1) {
        int length = IoTUartRead(NT1_DTU_UART, NT1_uartBuffPtr, NT1_UART_BUFF_SIZE);
        IoTUartWrite(0, NT1_uartBuffPtr, length);
        mm_LogPrint("[HMNetDTU]uart2 length:%d\r\n",length);
        // mm_print_hex(NT1_uartBuff,length);

        memcpy((char *)NT1_uartStrPtr+NT1_uartLength,(char *)NT1_uartBuffPtr,length);
        NT1_uartLength = NT1_uartLength + length;

        if(length < NT1_UART_BUFF_SIZE || NT1_uartBuff[15] == 0x0A){        // 会话结束
            memcpy((char *)NT1_uartDataPtr,(char *)NT1_uartStrPtr,NT1_uartLength);
            memcpy((char *)service_uartDataPtr,(char *)NT1_uartStrPtr,NT1_uartLength);// service
            service_ReadUartLength = NT1_uartLength;// service
            NT1_uartEndDataLength = NT1_uartLength;
            NT1_uartLength = 0;
            NT1_sendOSEvent();
            Service_sendOSEvent();
            memset(NT1_uartStr,0,NT1_UART_STR_SIZE);
        }

        memset(NT1_uartBuff,0,NT1_UART_BUFF_SIZE);
    }

    return NULL;
}

boolean NT1_SendHello(void){
    // memset(NT1_uartData,0,NT1_UART_Data_SIZE);
    // IoTUartWrite(NT1_DTU_UART, NT1_sockind, strlen(NT1_sockind));
    // osDelay(10);
    // IoTUartWrite(NT1_DTU_UART, NT1_sleep, strlen(NT1_sleep));
    // osDelay(10);
    // IoTUartWrite(NT1_DTU_UART, NT1_save, strlen(NT1_save));  
    // osDelay(10);
    // NT1_uartEndDataLength = 0;
    // NT1_uartLength = 0;
    // memset(NT1_uartData,0,NT1_UART_Data_SIZE);
    // memset(NT1_uartStr,0,NT1_UART_STR_SIZE);
    // memset(NT1_uartBuff,0,NT1_UART_BUFF_SIZE);
    return FALSE;
}

char* NT1_SendMsg(char * sendData,int ** getDataLength,int sendDataLength){
    memset(NT1_uartData,0,NT1_UART_Data_SIZE);
    osEventFlagsClear(NT1_evtID,NT1_FLAGS_MSK1);
    IoTUartWrite(NT1_DTU_UART, sendData, sendDataLength);
    osEventFlagsWait(NT1_evtID, NT1_FLAGS_MSK1, osFlagsWaitAny, 2000);
    *getDataLength = NT1_uartEndDataLength + 0;
    NT1_uartEndDataLength = 0;
    return (char*)NT1_uartData;
}

void NT1_sendOSEvent(void){
    osEventFlagsSet(NT1_evtID, NT1_FLAGS_MSK1);
    osThreadYield();
}

void NT1_ConfigThread(void){
    NT1_attr.name = "NT1_Config_Task";
    NT1_attr.attr_bits = 0U;
    NT1_attr.cb_mem = NULL;
    NT1_attr.cb_size = 0U;
    NT1_attr.stack_mem = NULL;
    NT1_attr.stack_size = NT1_RX_TASK_STACK_SIZE;
    NT1_attr.priority = NT1_RX_TASK_PRIO;

    if (osThreadNew((osThreadFunc_t)NT1_Config_Task, NULL, &NT1_attr) == NULL) {
        mm_LogPrint("[HMNetDTU] Falied to create NT1_Config_Task!\n");
    }

    NT1_evtID = osEventFlagsNew(NULL);



    service_init();
}